package com.ai.service.networking.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.comm.SysNetty;
import com.ai.mapper.comm.SysNettyMapper;
import com.ai.model.vo.SysNettyLinkVo;
import com.ai.service.networking.ISysNettyService;
import com.ai.util.ConvertBeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 *
 * @author chen
 * @since 2023-04-03 21:17:12
 */
@Service
public class SysNettyServiceImpl extends ServiceImpl<SysNettyMapper, SysNetty> implements ISysNettyService {

    /**
     * 根据IP和端口号获取netty
     *
     * @param ip
     * @param port
     * @return
     */
    @Override
    public SysNetty getByIP(String ip, int port) {
        List<SysNetty> sysNettyList = list(new QueryWrapper<SysNetty>().eq("ip", ip));
        if (sysNettyList.size() == 1) {
            return sysNettyList.get(0);
        }
        return getOne(new QueryWrapper<SysNetty>().eq("ip", ip).eq("port", port));
    }

    /**
     * 根据code获取netty
     *
     * @param code
     * @return
     */
    @Override
    public SysNetty getByteCode(String code) {
        return getOne(new QueryWrapper<SysNetty>().eq("code", code));
    }

    /**
     * 获取本机的所有服务端
     */
    @Override
    public List<SysNetty> getNativeServerList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 1));
    }

    /**
     * 获取本机的所有客户端
     */
    @Override
    public List<SysNetty> getNativeClientLIst() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 2));
    }


    /**
     * 获取本机的所有自启动的服务端
     */
    @Override
    public List<SysNetty> getNativeStartServerList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 1).eq("is_start", 1));
    }

    /**
     * 获取本机的素有自启动的客户端
     */
    @Override
    public List<SysNetty> getNativeStartClientList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 1).eq("netty_type", 2).eq("is_start", 1));
    }


    /**
     * 获取非本机的所有服务端
     */
    @Override
    public List<SysNetty> getNonNativeServerList() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 2).eq("netty_type", 1));
    }

    /**
     * 获取非本机的所有客户端
     */
    @Override
    public List<SysNetty> getNonNativeClientLIst() {
        return list(new QueryWrapper<SysNetty>().eq("is_local", 2).eq("netty_type", 2));
    }

    @Override
    public CommonResponse addNewComLink(SysNettyLinkVo nettyLinkVo) {

        //首先检测当前链路code是否已经存在
        SysNetty dbCodeLink = this.getByteCode(nettyLinkVo.getCode());
        if (!Objects.isNull(dbCodeLink)){
            return CommonResponse.error("当前code已经存在");
        }

        //检测当前链路是否已经存在
        QueryWrapper<SysNetty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("port",nettyLinkVo.getPort());
        queryWrapper.eq("ip",nettyLinkVo.getIp());

        SysNetty dbLink = this.getOne(queryWrapper);
        if (Objects.isNull(dbLink)){
            return CommonResponse.error("当前链路已存在");
        }
        //新增链路
        SysNetty sysNetty = ConvertBeanUtils.convert(nettyLinkVo, SysNetty.class);

        boolean save = this.save(sysNetty);

        if (save){
            return CommonResponse.ok("链路新增成功");
        }else {
            return CommonResponse.ok("链路新增失败");
        }

    }
}
